import com.zgroup.common.CipherUtils
import com.zgroup.domain.Setting
import com.zgroup.domain.SysUser
import com.zgroup.domain.Role


class BootStrap {
  def init = {
    System.setProperty("jmagick.systemclassloader", "no");

    if (!Setting.count()) {
      new Setting().save()
    }

    if (!Role.count()) {
      def role = new Role()
      role.roleName = "administrator"
      role.roleDesc = "administrator"
      role.permission = "01111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"

      if (!role.save()) {
        role.errors.each {
          println it
        }
      }
    }

    // 默认系统管理员初始化
    if (!SysUser.count()) {
      def user = new SysUser()
      user.username = "admin"
      user.password = CipherUtils.md5("admin")
      user.name = "admin"
      user.email = "admin@doulai123.com"

      //权限 开放所有权限
      Role role = Role.get(1)

      user.role = role
      println("-----" + role)
      println("-----" + user.role)
      if (!user.save()) {
        user.errors.each {
          println "***************" + it
        }
      }
      println("-----++++++")

    }
  }

  def destroy = {
  }
} 